<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="班级">
              <j-search-select-tag placeholder="请选择班级" v-model="queryParam.classId"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="面试人">
              <j-search-select-tag placeholder="请选择面试人" v-model="queryParam.stuId"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="面试老师">
                <j-search-select-tag placeholder="请选择面试老师" v-model="queryParam.teacherId" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="面试结果">
                <j-dict-select-tag placeholder="请选择面试结果" v-model="queryParam.mockInterviewResult" dictCode="mock_interview_result"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('模拟面试表')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>

              <a-menu-item v-if="record.mockInterviewStatus=='1'">
                <a @click="makeStatus(record,2)">发布模拟面试</a>
              </a-menu-item>
              <a-menu-item v-if="record.mockInterviewStatus=='2'">
                <a @click="makeStatus(record,3)">结束模拟面试</a>
              </a-menu-item>
              <a-menu-item v-if="record.mockInterviewStatus=='2'">
                <a @click="makeAdvise(record)">发布意见</a>
              </a-menu-item>
              <a-menu-item v-if="record.mockInterviewStatus=='3' && record.mockInterviewResult==null">
                <a @click="makeInterviewResult(record,2)">面试通过</a>
              </a-menu-item>
              <a-menu-item v-if="record.mockInterviewStatus=='3' && record.mockInterviewResult==null">
                <a @click="makeInterviewResult(record,1)">面试不合格</a>
              </a-menu-item>
              <a-menu-item v-if="record.mockInterviewResult=='1'">
                <a @click="againInterviewResult(record)">再次面试</a>
              </a-menu-item>

              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <mock-interview-modal ref="modalForm" @ok="modalFormOk"></mock-interview-modal>

    <a-modal
      title="Title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-row>
        <a-col :span="24">
          <a-form-model-item label="老师意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teacherAdvise">
            <j-editor v-model="teacherAdvise" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import MockInterviewModal from './modules/MockInterviewModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import {getAction, postAction} from "../../../api/manage";
  import {filterDictText, initDictOptions} from "../../../components/dict/JDictSelectUtil";

  export default {
    name: 'MockInterviewList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      MockInterviewModal
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        description: '模拟面试表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'编号',
            align:"center",
            dataIndex: 'mockInterviewId'
          },
          {
            title:'班级',
            align:"center",
            dataIndex: 'classId',
            customRender:(text)=>{
              for (let i = 0; i < this.clsList.length; i++) {
                if (text==this.clsList[i].classId){
                  return this.clsList[i].className
                }
              }
            }
          },
          {
            title:'面试人',
            align:"center",
            dataIndex: 'stuId',
            customRender:(text)=>{
              for (let i = 0; i < this.stuList.length; i++) {
                if (text==this.stuList[i].stuId){
                  return this.stuList[i].stuName
                }
              }
            }
          },
          {
            title:'面试老师',
            align:"center",
            dataIndex: 'teacherId',
            customRender:(text)=>{
              for (let i = 0; i < this.teacherList.length; i++) {
                if (text==this.teacherList[i].teacherId){
                  return this.teacherList[i].teacherName
                }
              }
            }
          },
          {
            title:'面试时间',
            align:"center",
            dataIndex: 'mockInterviewTime'
          },
          {
            title:'老师意见',
            align:"center",
            dataIndex: 'teacherAdvise',
            scopedSlots: {customRender: 'htmlSlot'}
          },
          {
            title:'学生总结',
            align:"center",
            dataIndex: 'stuConclusion',
            scopedSlots: {customRender: 'htmlSlot'}
          },
          {
            title:'模拟面试状态',
            align:"center",
            dataIndex: 'mockInterviewStatus',
            customRender: (text, record, index) => {
              //字典值替换通用方法
              return filterDictText(this.mockInterviewStatusList, text);
            }
          },
          {
            title:'面试结果',
            align:"center",
            dataIndex: 'mockInterviewResult',
            customRender: (text, record, index) => {
              //字典值替换通用方法
              return filterDictText(this.mockInterviewResultList, text);
            }
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/mockinterview/mockInterview/list",
          delete: "/mockinterview/mockInterview/delete",
          deleteBatch: "/mockinterview/mockInterview/deleteBatch",
          exportXlsUrl: "/mockinterview/mockInterview/exportXls",
          importExcelUrl: "mockinterview/mockInterview/importExcel",
          getStuListUrl:"/stu/stu/getStuList",
          getTeacherListUrl:"/teacher/teacher/getTeacherList",
          getClsListUrl:"/cls/cls/getClsList",
          editMockInterview:"/mockinterview/mockInterview/edit",
          addMockInterview:"/mockinterview/mockInterview/add",
        },
        dictOptions:{},
        superFieldList:[],
        stuList:[],
        teacherList:[],
        clsList:[],
        mockInterviewStatusList:[],
        mockInterviewResultList:[],
        mockInterview:{},
        teacherAdvise:""
      }
    },
    created() {
      this.getSuperFieldList();
      this.getStuList();
      this.getClsList();
      this.getTeacherList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      /*再次模拟面试*/
      againInterviewResult(record){
        let math=Math.round(Math.random()*100000000)
        let id = new Date().getTime().toString()+math
        this.mockInterview=record
        this.mockInterview.id=id
        this.mockInterview.teacherAdvise=null
        this.mockInterview.stuConclusion=null
        this.mockInterview.mockInterviewStatus="1"
        this.mockInterview.mockInterviewResult=null
        postAction(this.url.addMockInterview,this.mockInterview).then((resp)=>{
          if (resp.success){
            this.$message.success('添加成功');
          }else {
            this.$message.warning('添加失败');
          }
          this.$router.go(0)
        })
      },
      /*发布意见*/
      handleOk(e) {
        this.mockInterview.teacherAdvise=this.teacherAdvise
        postAction(this.url.editMockInterview,this.mockInterview).then((resp)=>{
          if (resp.success){
            this.$message.success('修改成功');
          }else {
            this.$message.warning('修改失败');
          }
        })
        this.confirmLoading = true;
        setTimeout(() => {
          this.visible = false;
          this.confirmLoading = false;
        }, 2000);
      },
      handleCancel(e) {
        console.log('Clicked cancel button');
        this.visible = false;
      },


      /*修改模拟面试状态*/
      makeStatus(record,mockInterviewStatus){
        this.mockInterview=record
        this.mockInterview.mockInterviewStatus=mockInterviewStatus
        postAction(this.url.editMockInterview,this.mockInterview).then((resp)=>{
          if (resp.success){
            this.$message.success('修改成功');
          }else {
            this.$message.warning('修改失败');
          }
        })
      },
      /*面试意见弹框*/
      makeAdvise(record){
        this.visible = true;
        this.mockInterview=record
      },
      /*修改面试结果*/
      makeInterviewResult(record,interviewResult){
        this.mockInterview=record
        this.mockInterview.mockInterviewResult=interviewResult
        postAction(this.url.editMockInterview,this.mockInterview).then((resp)=>{
          if (resp.success){
            this.$message.success('修改成功');
          }else {
            this.$message.warning('修改失败');
          }
        })
      },
      /*查询学生*/
      getStuList(){
        getAction(this.url.getStuListUrl).then((resp)=>{
          if (resp.success){
            this.stuList=resp.result
          }
        })
      },
      /*查询老师*/
      getTeacherList(){
        getAction(this.url.getTeacherListUrl).then((resp)=>{
          if (resp.success){
            this.teacherList=resp.result
          }
        })
      },
      /*查询班级*/
      getClsList(){
        getAction(this.url.getClsListUrl).then((resp)=>{
          if (resp.success){
            this.clsList=resp.result
          }
        })
      },
      initDictConfig(){
        /*面试状态*/
        initDictOptions('mock_interview_status').then((res) => {
          if (res.success) {
            this.mockInterviewStatusList = res.result;
          }
        });
        /*面试结果*/
        initDictOptions('mock_interview_result').then((res) => {
          if (res.success) {
            this.mockInterviewResultList = res.result;
          }
        });
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'mockInterviewId',text:'编号',dictCode:''})
        fieldList.push({type:'sel_search',value:'classId',text:'班级',dictTable:"", dictText:'', dictCode:''})
        fieldList.push({type:'sel_search',value:'stuId',text:'面试人',dictTable:"", dictText:'', dictCode:''})
        fieldList.push({type:'sel_search',value:'teacherId',text:'面试老师',dictTable:"", dictText:'', dictCode:''})
        fieldList.push({type:'datetime',value:'mockInterviewTime',text:'面试时间'})
        fieldList.push({type:'string',value:'teacherAdvise',text:'老师意见',dictCode:''})
        fieldList.push({type:'string',value:'stuConclusion',text:'学生总结',dictCode:''})
        fieldList.push({type:'string',value:'mockInterviewStatus',text:'模拟面试状态',dictCode:'mock_interview_status'})
        fieldList.push({type:'string',value:'mockInterviewResult',text:'面试结果',dictCode:'mock_interview_result'})
        fieldList.push({type:'string',value:'createBy',text:'创建人',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>